//链接数据表
const addService = require('../service/user')
const utils = require('../extend/helper')
//添加
const addUserController = async(ctx,next)=>{
    //添加的语句
    let result = await addService.addUserService(ctx.request.body)
    console.log(result,'result')
     if(result){
         ctx.body={
             code:200,
             mag:'addUserController,success',
             data:result
         }
     }else {
         ctx.body={
             code:403,
             mag:'params'
         }
     }
 }
 //查询
const getUserController = async(ctx,next)=>{
    //查看
    let result = await addService.getUserService()
    console.log(result,'result0')
     if(result){
         ctx.body={
             code:200,
             mag:'getUserController,success',
             data:result
         }
     }else {
         ctx.body={
             code:403,
             mag:'params'
         }
     }
 }

//修改
const updataUserController = async(ctx,next)=>{
    console.log(ctx.request.body)
    //查看
    let result = await addService.updataUserService()
    console.log(result,'result0')
     if(result){
         ctx.body={
             code:200,
             mag:'updataUserController,success',
             data:result
         }
     }else {
         ctx.body={
             code:403,
             mag:'params'
         }
     }
 }

//删除
const deleteUserController = async(ctx,next)=>{
    //找到要删除的id
    let result = await addService.addUserService(ctx.params.id);
    if(result){
        ctx.body={
            code:200,
            mag:'deleteUserController,success',
            data:result
        }
    }else {
        ctx.body={
            code:403,
            mag:'params'
        }
    }
 }


// mysql
//查看数据
const getListController = async(ctx,next)=>{
    let result = await addService.getList()
    console.log(result,"result1")
    ctx.body={
        code:200,
        msg:'get success'
    }
}
//添加数据
const addListController = async(ctx,next)=>{
    console.log(ctx.request.body)
    let result = await addService.addList(ctx.request.body)
    console.log(result,"result2")
    if(result){
        ctx.body={
            code:200,
            msg:'add success',
            data:null
            
        }
    }else {
        ctx.body={
            code:403,
            msg:' err'
        }
    }
}

//更改数据
const updataListController = async(ctx,next)=>{
    console.log(ctx.request.query,'-----')
    let result = await addService.updataList(ctx.request.query)
    console.log(result,"result3")
    if(result){
        ctx.body={
            code:200,
            msg:'updata success',
            data:null
            
        }
    }else {
        ctx.body={
            code:403,
            msg:' err'
        }
    }
}

//删除数据
const delListController = async(ctx,next)=>{
    console.log(ctx.request.body,'-----')
    let result = await addService.delList(ctx.request.body)
    console.log(result,"result4")
    //查出 有数据显示
    if(result){
        ctx.body={
            code:200,
            msg:'del success',
            data:null
            
        }
    }else {
        ctx.body={
            code:403,
            msg:' err'
        }
    }
}

//注册
const registryUsersController = async(ctx,next)=>{
    // console.log( ctx.request.body.pwd);
    //查找的密码等于加密的密码
    ctx.request.body.pwd = await utils.signPassword(ctx.request.body.pwd)
    let result = await addService.registryUsersService(ctx.request.body)
    if(result){
        ctx.body={
            code:200,
            msg:'success',
            data:null
        }
    }else {
        ctx.body={
            code:403,
            msg:' err'
        }
    }
}
//登录
const loginUsersController = async(ctx,next)=>{
    //查找的密码等于加密的密码
    ctx.request.body.pwd = await utils.signPassword(ctx.request.body.pwd)
    let result = await addService.loginUsersService(ctx.request.body)
    if(result.status){
        //
        let token = await utils.signToken(result.data)
        ctx.body={
            code:200,
            msg:'success',
            //返回加密的token
            data:token
        }
    }else {
        ctx.body={
            code:403,
            msg:result.message,
            data:null
        }
    }
}
module.exports={
    addUserController,
    deleteUserController,
    getUserController,
    updataUserController,
    getListController,
    addListController,
    updataListController,
    delListController,

    registryUsersController,
    loginUsersController
}